<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0">
    <title>考试系统 - Subjects</title>

    <!-- Favicon -->
    <link rel="shortcut icon" href="${pageContext.request.contextPath}/static/img/favicon.png">

    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,500;0,600;0,700;1,400&display=swap">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/plugins/bootstrap/css/bootstrap.min.css">

    <!-- Fontawesome CSS -->
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/plugins/fontawesome/css/fontawesome.min.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/plugins/fontawesome/css/all.min.css">

    <!-- Datatables CSS -->
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/plugins/datatables/datatables.min.css">

    <!-- Main CSS -->
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/style.css">
</head>
<body>

<!-- Main Wrapper -->
<div class="main-wrapper">


    <!-- Header -->
    <%@include file="/pages/common/header.jsp"%>
    <!-- /Header -->

    <!-- Sidebar -->
    <%@include file="/pages/common/sidebar.jsp"%>
    <!-- /Sidebar -->

    <!-- Page Wrapper -->
    <div class="page-wrapper">
        <div class="content container-fluid">

            <!-- Page Header -->
            <div class="page-header">
                <div class="row align-items-center">
                    <div class="col">
                        <h3 class="page-title">试题管理</h3>
                        <ul class="breadcrumb">
                            <li class="breadcrumb-item"><a href="index.jsp">试卷</a></li>
                            <li class="breadcrumb-item active">试题管理</li>
                        </ul>
                    </div>

                    <div class="col-auto text-right float-right ml-auto">
                        <button class="btn btn-rounded btn-outline-success" onclick="addQuestionToPaper()"><i class="fas fa-plus"></i> 手动添加</button>
<%--                        <button class="btn btn-rounded btn-outline-warning" onclick="randomAddQuestionToPaper()">自动生成</button>--%>
                    </div>
                </div>
            </div>
            <script>
                function addQuestionToPaper() {
                    window.location.href="${pageContext.request.contextPath}/questionServlet?action=findQuestions&paperId=${paperId}"
                }

                function randomAddQuestionToPaper() {
                    window.location.href="${pageContext.request.contextPath}/pages/add-question-paper.jsp?paperId=${paperId}";
                }

            </script>
            <!-- /Page Header -->

            <div class="row">
                <div class="col-sm-12">

                    <div class="card card-table">
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-hover table-center mb-0 datatable">
                                    <thead>
                                    <tr>
                                        <th>试题Id</th>
                                        <th>类型</th>
                                        <th>课程类型</th>
                                        <th>题目</th>
                                        <th>选项A</th>
                                        <th>选项B</th>
                                        <th>选项C</th>
                                        <th>选项D</th>
                                        <th>正确答案</th>
                                        <th class="text-right">操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <c:forEach items="${questions}" var="question">
                                        <tr>
                                            <td>${question.question_id}</td>
                                            <c:if test="${question.question_type=='x'}">
                                                <td>单选题</td>
                                            </c:if>
                                            <c:if test="${question.question_type=='y'}">
                                                <td>多选题</td>
                                            </c:if>
                                            <c:if test="${question.question_type=='z'}">
                                                <td>判断题</td>
                                            </c:if>
                                            <td>${question.course_name}</td>

                                            <c:if test="${fn:length(question.question_name) >= 9}">
                                                <td>${fn:substring(question.question_name,0,7)}...</td>
                                            </c:if>
                                            <c:if test="${fn:length(question.question_name) <9}">
                                                <td>${question.question_name}</td>
                                            </c:if>

                                            <c:if test="${fn:length(question.question_opa) >= 5}">
                                                <td>${fn:substring(question.question_opa,0,5)}...</td>
                                            </c:if>
                                            <c:if test="${fn:length(question.question_opa) < 5}">
                                                <td>${question.question_opa}</td>
                                            </c:if>

                                            <c:if test="${fn:length(question.question_opb) >= 5}">
                                                <td>${fn:substring(question.question_opb,0,5)}...</td>
                                            </c:if>
                                            <c:if test="${fn:length(question.question_opb) < 5}">
                                                <td>${question.question_opb}</td>
                                            </c:if>

                                            <c:if test="${fn:length(question.question_opc) >= 5}">
                                                <td>${fn:substring(question.question_opc,0,5)}...</td>
                                            </c:if>
                                            <c:if test="${fn:length(question.question_opc) < 5}">
                                                <td>${question.question_opc}</td>
                                            </c:if>

                                            <c:if test="${fn:length(question.question_opd) >= 5}">
                                                <td>${fn:substring(question.question_opd,0,5)}...</td>
                                            </c:if>
                                            <c:if test="${fn:length(question.question_opd) < 5}">
                                                <td>${question.question_opd}</td>
                                            </c:if>

                                            <td>${question.question_opright}</td>
                                            <td class="text-right">
                                                <div class="actions">
                                                    <a href="${pageContext.request.contextPath}/questionServlet?action=findQuestionById&question_id=${question.question_id}" class="btn btn-sm bg-success-light mr-2">
                                                        <i class="fas fa-pen"></i>
                                                    </a>
                                                    <a href="javascript:;" onclick="deleteQuestionInPaper('${question.question_name}',${question.question_id})"  class="btn btn-sm bg-danger-light">
                                                        <i class="fas fa-trash"></i>
                                                    </a>
                                                </div>
                                            </td>
                                        </tr>
                                    </c:forEach>

                                    </tbody>
                                </table>
                                <script src="${pageContext.request.contextPath}/static/js/jquery-3.5.1.min.js"></script>
                                <script>
                                    function deleteQuestionInPaper(name,question_id){
                                        if (confirm("是否要从试卷删除题目\"" + name + "\"?")) {
                                            $.post("${pageContext.request.contextPath}/questionServlet",{"action":"deleteQuestionInPaper","question_id":question_id,"paperId":${paperId}}
                                                ,function (data) {
                                                    if (data.result){
                                                        alert("删除成功！")
                                                        window.location.href="${pageContext.request.contextPath}/paperServlet?action=findPaperQuestions&paperId=${paperId}"
                                                    }
                                                },"json")
                                        }else {
                                            return false;
                                        }
                                    }
                                </script>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Footer -->
        <footer>
            <p>交通规则在线模拟考试系统 </p>
        </footer>
        <!-- /Footer -->

    </div>
    <!-- /Page Wrapper -->

</div>
<!-- /Main Wrapper -->

<!-- jQuery -->
<script src="${pageContext.request.contextPath}/static/js/jquery-3.5.1.min.js"></script>

<!-- Bootstrap Core JS -->
<script src="${pageContext.request.contextPath}/static/js/popper.min.js"></script>
<script src="${pageContext.request.contextPath}/static/plugins/bootstrap/js/bootstrap.min.js"></script>

<!-- Slimscroll JS -->
<script src="${pageContext.request.contextPath}/static/plugins/slimscroll/jquery.slimscroll.min.js"></script>

<!-- Datatables JS -->
<script src="${pageContext.request.contextPath}/static/plugins/datatables/datatables.min.js"></script>

<!-- Custom JS -->
<script src="${pageContext.request.contextPath}/static/js/script.js"></script>
</body>
</html>
